<?php

register_shutdown_function(function() {
  $lastError = error_get_last();
  if (!is_null($lastError)) {
    print_r($lastError);
  }
});

include 'config.inc.php';
include _ITSTOCK_LIB_DIR_.'common.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_base.inc.php';
include _ITSTOCK_INCLUDE_DIR_.'html_options.inc.php';

itstock_session_start();

function itstock_request_popup($itstock_page_info) {

  if (file_exists(_ITSTOCK_PAGE_DIR_. $itstock_page_info['page'] . ".inc.php")) {
    include _ITSTOCK_PAGE_DIR_. $itstock_page_info['page'] . ".inc.php";
  } else {
    header("HTTP/1.1 500 Internal Server Error", true, 500);
    printf("<b>Page Not Found.</b>");
    exit();
  }
  if(isset($itstock_page_info['css']) || isset($itstock_page_info['js'])) {
    printf("<div id='header_files'>");
    itstock_html_print_css($itstock_page_info);
    itstock_html_print_js($itstock_page_info);
    printf("</div>");
  }
  itstock_db_close_all();
}

if (isset($_GET["page"])) {
  if (!itstock_valid_login($_SESSION['user'])) {
    header("HTTP/1.1 403 Forbidden", true, 500);
    $_SESSION['error'] = "Your session has expired. Please re-login.";
    exit();
  }
  itstock_request_popup($_GET);
  exit();
}

if (isset($_POST["type"]) && isset($_POST["name"]) && isset($_POST["field"]) && isset($_POST["value"])) {
  if (!itstock_valid_login($_SESSION['user'])) {
    header("HTTP/1.1 403 Forbidden", true, 500);
    $_SESSION['error'] = "Your session has expired. Please re-login.";
    exit();
  }
  itstock_structure($struct, $_POST["type"]);
  foreach($_POST["field"] as $i => $field) {
    $struct['where'][$i] = $field.'='.itstock_decode($_POST["value"][$i]);
  }
  $null = null;
  $struct['limit'] = '';
  itstock_html_options(null, $_POST["name"], '',  $null, false, $struct);
  itstock_db_close_all();
  exit();
}

header("Location: index.php");
?>
